package com.uit.httt04.duongvanlam.facedetection.classes;

import static com.googlecode.javacv.cpp.opencv_core.*;

import com.uit.httt04.duongvanlam.facedetection.imageprocessing.ImageProcessing;

public class ObjectAlgorithm extends Object
{
	private int id;
	private IplImage image;
	private String imagePath;

	//
	// <-- Debug
	//
	// public CvMat Matrix;

	//
	// Debug -->
	//

	public ObjectAlgorithm(CvMat Matrix, int Id, String ImagePath) throws Exception
	{
		try
		{
			this.image = cvCreateImage(cvSize(Matrix.rows(), Matrix.cols()), IPL_DEPTH_32F, 1);
			//
			//
			//
			CvMat MatrixCopy = cvCreateMat(Matrix.rows(), Matrix.cols(), CV_32FC1);
			for (int i = 0; i < Matrix.rows(); i++)
				for (int j = 0; j < Matrix.cols(); j++)
					MatrixCopy.put(i, j, Matrix.get(i, j));

			this.image = cvGetImage(MatrixCopy, this.image);
			//
			//
			//
			this.id = Id;
			this.imagePath = ImagePath;

			//
			// <-- Debug
			//
			// this.Matrix = this.image.asCvMat();
			//
			// Debug -->
			//
		} catch (Exception ex)
		{
			throw ex;
		}
	}

	public ObjectAlgorithm(String ImagePath, int Id, int newHeight, int newWidth) throws Exception
	{
		try
		{
			this.imagePath = ImagePath;
			this.id = Id;

			IplImage scaledImage = ImageProcessing.LoadAndScale(this.imagePath, newHeight, newWidth);
			this.image = ImageProcessing.EqualizeHist(scaledImage);

			//
			// <-- Debug
			//
			// this.Matrix = this.image.asCvMat();
			//
			// Debug -->
			//

			cvReleaseImage(scaledImage);
			System.gc();
		} catch (Exception ex)
		{
			throw ex;
		}
	}

	public CvMat getMatrix()
	{
		return this.image.asCvMat();
	}

	public int getID()
	{
		return this.id;
	}

	public String getPersonName()
	{
		return ImageAndFace.getPersonNameByImageName(this.getImageName());
	}

	public String getImageName()
	{
		String ImageName = this.imagePath.substring(this.imagePath.lastIndexOf("/") + 1, this.imagePath.lastIndexOf("."));
		return ImageName;
	}

	public String getImagePath()
	{
		return this.imagePath;
	}

	@Override
	public void finalize() throws Throwable
	{
		cvReleaseImage(image);
		this.imagePath = null;
		super.finalize();
	}
}
